using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PrintScreen;
using System.IO;

namespace PrintScreenFull
{
    public partial class frmPrintScreen : Form
    {
        public frmPrintScreen()
        {
            InitializeComponent();
        }

        private void frmPrintScreen_Load(object sender, EventArgs e)
        {
            regHotKeyPrintScreen1.Key = Keys.PrintScreen;
            regHotKeyPrintScreen1.KeyModifier = PrintScreen.KeyModifiers.All;
            this.WindowState = FormWindowState.Minimized;
            this.Visible = false;
            chkStartup.Checked = CheckStartup();
            notifyIcon1.Text = "Press Print Screen";
            notifyIcon1.ShowBalloonTip(2000);
        }

        private bool CheckStartup()
        {
            bool ret = false;
            string folder = System.Environment.GetFolderPath(Environment.SpecialFolder.Startup);
            if (!Directory.Exists(folder))
                return ret;

            if (!(Directory.GetFiles(folder, "printscreen*").Length > 0))
                return ret;

            ret = true;
            return ret;

        }

        private void regHotKeyPrintScreen1_HotKeyPressed(object sender, KeyEventArgs e)
        {
            ShowVisible();

        }

        

        private void btnEmail_Click(object sender, EventArgs e)
        {
            ActionToDo_Email a = new ActionToDo_Email(regHotKeyPrintScreen1.Image);            
            a.DoAction();
            this.WindowState = FormWindowState.Minimized;
        }

        private void frmPrintScreen_ResizeEnd(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Visible = false;                
            }
        }
        private void ShowVisible()
        {
            this.Visible = true;
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.CenterToScreen();
            this.Activate();
            this.Focus();
        }
        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.Visible == false)
            {
                ShowVisible();
            }
            else
            {
                this.Visible = false;
            }
        }

        private void frmPrintScreen_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Visible = false;
            }
        }

        private void btnOpenFiles_Click(object sender, EventArgs e)
        {
            ActionToDo_RunFiles r  = new ActionToDo_RunFiles(regHotKeyPrintScreen1.Image);
            r.DoAction();
            this.WindowState = FormWindowState.Minimized;
        }

        

        private void btnOpenFolder_Click(object sender, EventArgs e)
        {
            ActionToDo_RunDir r = new ActionToDo_RunDir(regHotKeyPrintScreen1.Image);
            r.DoAction();
            this.WindowState = FormWindowState.Minimized;
        }
        /// <summary>
        /// http://www.sorrowman.org/c-sharp-programmer/url-link-to-desktop.html
        /// </summary>
        /// <param name="linkName"></param>
        private void appShortcut(string Path ,string linkName)
        {


            using (StreamWriter writer = new StreamWriter(Path + "\\" + linkName + ".url"))
            {
                string app = System.Reflection.Assembly.GetExecutingAssembly().Location;
                writer.WriteLine("[InternetShortcut]");
                writer.WriteLine("URL=file:///" + app);
                writer.WriteLine("IconIndex=0");
                string icon = app.Replace('\\', '/');
                writer.WriteLine("IconFile=" + icon);
                writer.Flush();
            }
        }

        private void chkStartup_CheckedChanged(object sender, EventArgs e)
        {
            switch (chkStartup.Checked)
            {
                case true: 
                    appShortcut(System.Environment.GetFolderPath(Environment.SpecialFolder.Startup),"printscreen");
                    break;
                case false:
                    string strFile = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Startup),"printscreen");
                    if (File.Exists(strFile))
                        File.Delete(strFile);

                    break;
            }
        }

        private void btnMinimize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }              
    }
}